﻿using AutoMapper;
using Microsoft.EntityFrameworkCore;
using MyTask.Domain.Models;
using MyTask.Infrastructure.Context;
using System;
using System.Collections.Generic;
using System.Text;

namespace MyTask.Domain.Commands.Handlers
{
   public class TaskInfoCommandHandler
    {
        #region Private variable
        //DbContext
        private readonly MyTaskContext _context;
        #endregion
        #region Constructor
        public TaskInfoCommandHandler(MyTaskContext context)
        {
            _context = context;
        }
        #endregion
        #region Public method
        /// <summary>
        /// add task
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public bool AddTaskInfo(TaskInfoCommand command)
        {
            var config = new MapperConfiguration(cfg => cfg.CreateMap<TaskInfoCommand, TaskInfo>());
            IMapper mapper = config.CreateMapper();
            TaskInfo taskInfo = mapper.Map<TaskInfoCommand, TaskInfo>(command);
            _context.TaskInfos.Add(taskInfo);
            var result = _context.SaveChanges() > 0;
            return result;
        }
        /// <summary>
        /// update task
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public bool UpdateTaskInfo(TaskInfoCommand command)
        {
            command.IsComplete = true;
            var config = new MapperConfiguration(cfg => cfg.CreateMap<TaskInfoCommand, TaskInfo>());
            IMapper mapper = config.CreateMapper();
            TaskInfo taskInfo = mapper.Map<TaskInfoCommand, TaskInfo>(command);
            _context.Entry<TaskInfo>(taskInfo).State = EntityState.Modified;
            var result = _context.SaveChanges() > 0;
            return result;
        } 
        #endregion


    }
}
